<template>
  <drawer :show="edit" :styles="{width:'750px'}" pos="right" tran="push" @change-show="changeDrawerShow">
    <div slot="drawer" style="height:100%">
      <edit v-if="edit" :data="selectTable" @getData="getData" @close="edit=false"></edit>
    </div>
    <div class="app-container calendar-list-container">
      <div class="filter-container">
        <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;" placeholder="请输入关键词" v-model="search.keyword"></el-input>
        <div class="filter-item">
          <span class="txt">添加时间</span>
          <el-date-picker style="width: 240px;" value-format="timestamp" v-model="search.date" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </div>
        <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </div>
      <el-table v-loading="!pload" @selection-change="selectionChange" ref="multipleTable" border :data="tableData">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column min-width="180" align="center" label="类型" property="articleTypeName" show-overflow-tooltip></el-table-column>
        <el-table-column min-width="180" align="center" label="文章标题" property="articleTitle" show-overflow-tooltip></el-table-column>
        <el-table-column min-width="180" align="center" label="审核状态"  show-overflow-tooltip> 
          <template slot-scope="scope">
            <span v-if="scope.row.reviewStatus === -1">未审核</span>
            <span v-if="scope.row.reviewStatus === 0">不通过</span>
            <span v-if="scope.row.reviewStatus === 1">通过</span>
          </template>
        </el-table-column>
        <el-table-column min-width="180" align="center" label="原因" property="causeName" show-overflow-tooltip></el-table-column>
        <el-table-column label="添加时间" align="center" width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">{{scope.row.reviewStatus===-1?'审核':'查看'}}</el-button>
            <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="filter-bottom">
        <el-button-group class="filter-item">
          <el-button @click="toggleSelection(-1)">全选</el-button>
          <el-button @click="toggleSelection(0)" :disabled="!selection.length">反向选择</el-button>
          <el-button @click="toggleSelection(1)" :disabled="!selection.length">取消选择</el-button>
        </el-button-group>
        <el-button-group class="filter-item">
          <el-button @click="toggleDel" :disabled="!selection.length">删除选中数据</el-button>
        </el-button-group>
      </div>
      <div class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="limit" :page-sizes="limits" background layout="sizes, prev, pager, next, jumper, total" :total="count">
        </el-pagination>
      </div>
    </div>
  </drawer>
</template>

<script>
import * as api from '@/utils/api'
import waves from '@/directive/waves' // 水波纹指令
import edit from './edit'
export default {
  directives: {
    waves
  },
  data() {
    return {
      tableData: [],
      search: {
        keyword: ''
      },
      selection: [],
      selectTable: {},
      pload: false,
      edit: false,
      currentPage: 1,
      limit: 20,
      limits: [10, 20, 50, 100],
      count: 0
    }
  },
  components: {
    edit,
    drawer: () => import('@/components/Drawer')
  },
  created() {
    this.getData()
  },
  methods: {
    selectionChange(selection) {
      this.selection = selection
    },
    toggleSelection(s) {
      if (s === -1) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
        })
      }
      if (s === 0) {
        this.tableData.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      }
      if (s === 1) {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleAdd() {
      this.selectTable = { id: 0 }
      this.selectTable.method = 'add'
      this.edit = true
    },
    async handleDelete(index, row) {
      this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
        try {
          this.pload = false
          await api.ltsTipRemove({ tipIds: row.id })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    toggleDel() {
      this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
        const ids = this.selection.map((i, index) => {
          return i.id
        }).join(',')
        try {
          this.pload = false
          await api.ltsTipRemove({ tipIds: ids })
          this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
          this.getData()
        } catch (e) {
          this.pload = true
        }
      })
    },
    handleEdit(index, row) {
      this.selectTable = JSON.parse(JSON.stringify(row))
      this.selectTable.method = 'edit'
      this.edit = true
    },
    async getData() {
      if (this.search.date) {
        this.search.createTimeStart = this.search.date[0]
        this.search.createTimeEnd = this.search.date[1]
      } else {
        delete this.search.createTimeStart
        delete this.search.createTimeEnd
      }
      const data = this.JSONo(
        {
          pageNum: this.currentPage,
          pageSize: this.limit
        },
        this.search
      )
      this.pload = false
      const Datas = await api.ltsTipList(data)
      this.tableData = Datas.list
      this.count = Datas.total
      this.pload = true
    },
    handleFilter() {
      this.currentPage = 0
      this.getData()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.limit = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    },
    changeDrawerShow(state) {
      this.edit = state
      console.log(state)
    }
  }
}
</script>
